<?php
/**
 * class to build cli interface...
 * use listener to get user input
 * and map it to the actual functions afterwards
 *
 * @author bbqq <toughgamer@gmail.com>
 * @package com.blib.utility
 * @copyright bbqq qstufie.com
 */
class Blib_Utility_Cli extends Blib_Class_Atomic
{
    /**
     * the main function
     * to be extended by actual apps
     * @param array $opt options
     * @return mixed
     */
    public function main($opts)
    {
        Blib::dump($opts);

    }// end main


    /**
     * the listener core...
     * @param str $pattern opts pattern to read ops
     * @return void
     */
    public function listen($pattern = null)
    {
        // get options...
        if (!empty($pattern)) {
            $options = getopt($pattern);
            // and this means the function defaults to the main
            // function.
            return $this->main($options);
        }

        // otherwise, call function by simply allow the stuff
        // only up to 2 arguments for now
        if (isset($_SERVER['argv']) && isset($_SERVER['argv'][1])) {
            // control: 1, id: 2....
            $control = $_SERVER['argv'][1];
            $options = $_SERVER['argv'];
            unset($options[0], $options[1]);
            if (method_exists($this, $control)) {
                return call_user_func_array(array($this, $control), $options);
            }

            // otherwise, exceptioin...
            throw new exception("CONTROL: $control doesnt exist");
        }

        // otherwise... show options...
        $this->help();

     }// end listen


     /**
      * help function
      * @return void
      */
     public function help()
     {
        return;

     }// end help


     /**
      * hey dude, what's my process id?
      * @return int the pid
      */
     static public function getPid()
     {
        return getmypid();

     }// end getPid

}// end Blib_Utility_Cli
?>